set(__NXOS_BUILD_ARCH riscv64)

include(${CMAKE_CURRENT_LIST_DIR}/common.cmake)

set(CMAKE_SIZEOF_VOID_P 8)

# Get C compiler id.
execute_process(
  COMMAND ${CMAKE_C_COMPILER} --version
  OUTPUT_VARIABLE _c_compiler_version_output
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

if(_c_compiler_version_output MATCHES "clang")
  set(_need_march_ext_ver 17)
elseif(_c_compiler_version_output MATCHES "gcc")
  set(_need_march_ext_ver 12)
else()
  # unknown
  set(_need_march_ext_ver 100)
endif()

# Get C compiler major version.
execute_process(
  COMMAND ${CMAKE_C_COMPILER} -dumpversion
  OUTPUT_VARIABLE _c_compiler_version
  OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX MATCH "([0-9]+)\\." _ "${_c_compiler_version}")
set(_c_compiler_major_version ${CMAKE_MATCH_1})

if(_c_compiler_major_version GREATER_EQUAL ${_need_march_ext_ver})
  set(_riscv64_march rv64imafdc_zicsr_zifencei)
else()
  set(_riscv64_march rv64imafdc)
endif()

add_compile_options(-march=${_riscv64_march} -mabi=lp64d -mcmodel=medany)
add_link_options(-march=${_riscv64_march} -mabi=lp64d -mcmodel=medany)
